package com.mogist.hqc.module.community.ui

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import butterknife.OnClick
import butterknife.Optional
import com.chad.library.adapter.base.BaseQuickAdapter
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.entitys.BaseModel
import com.mogist.hqc.entitys.response.CommunityListVo
import com.mogist.hqc.entitys.response.CommunityVo
import com.mogist.hqc.module.community.adapter.CommunityAdapter
import com.mogist.hqc.module.community.contract.CommunityListContract
import com.mogist.hqc.module.community.presenter.CommunityListPresenter
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.util.Constant
import kotlinx.android.synthetic.main.activity_community.*
import kotlinx.android.synthetic.main.include_title.*
import talex.zsw.basecore.view.other.swipetoloadlayout.OnLoadMoreListener
import talex.zsw.basecore.view.other.swipetoloadlayout.OnRefreshListener
import talex.zsw.basecore.view.recyleview.DividerItemDecoration
import talex.zsw.basecore.view.recyleview.SampleFooter
import talex.zsw.basecore.view.recyleview.SampleHeader

/**
 * 作用：微社区
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class CommunityListActivity : BaseMVPActivity<CommunityListContract.Presenter>(), CommunityListContract.View, OnRefreshListener, OnLoadMoreListener
{
    override fun initArgs(intent: Intent)
    {
    }

    override fun initView()
    {
        setContentView(R.layout.activity_community)
        mPresenter = CommunityListPresenter(this)
    }

    override fun initData()
    {
        mTitleBar.setTitle("微社区")
        initRecyclerView()
        getData(false)
    }

    @Optional
    @OnClick(R.id.mIvNew)
    fun onViewClicked(view: View)
    {
        if (!isFastClick)
        {
            when (view.id)
            {
                R.id.mIvNew ->
                {
                    if (isLogin())
                    {
                        start(CommunityCreateActivity::class.java)
                    }
                }
            }
        }
    }

    private val adapter = CommunityAdapter()

    private fun initRecyclerView()
    {
        adapter.setOnItemClickListener { adapter, view, position ->
            if (!isFastClick)
            {
                val vo = adapter.getItem(position) as CommunityVo
                val intent = Intent(this@CommunityListActivity, CommunityDetailActivity::class.java)
                intent.putExtra("id", vo.id)
                start(intent)
            }
        }
        adapter.emptyView =
                SampleHeader(this@CommunityListActivity, swipe_target.width, swipe_target.height)
        adapter.isUseEmpty(false)
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN)
        swipe_target.addItemDecoration(DividerItemDecoration(this@CommunityListActivity,
                                                             DividerItemDecoration.VERTICAL_LIST,
                                                             R.color.window_background,
                                                             30,
                                                             true,
                                                             false))
        swipe_target.layoutManager = LinearLayoutManager(this@CommunityListActivity)
        swipe_target.adapter = adapter

        mSwipeToLoadLayout.setOnRefreshListener(this)
        mSwipeToLoadLayout.setOnLoadMoreListener(this)
        mSwipeToLoadLayout.isLoadMoreEnabled = true
        mSwipeToLoadLayout.isRefreshEnabled = true
    }

    // --------------- 数据获取 ---------------
    override fun onDestroy()
    {
        super.onDestroy()
        adapter.removeAllFooterView()
        sampleFooter = null
    }

    override fun getData(silence: Boolean)
    {
        val body = BaseModel(true)
        body.page = page.toString()
        mPresenter.getData(HttpDto(Constant.COMMUNITY_LIST, body).setType(HttpDto.GET))
    }

    var sampleFooter: SampleFooter? = null
    override fun bindData(data: CommunityListVo)
    {
        adapter.isUseEmpty(true)
        if (data.list != null && data.list.isNotEmpty())
        {
            if (page == 1)
            {
                adapter.replaceData(data.list)
            }
            else
            {
                adapter.addData(data.list)
            }
            if (data.pages == page)
            {
                sampleFooter
                        ?: let {
                            sampleFooter = SampleFooter(this@CommunityListActivity)
                        }
                adapter.setFooterView(sampleFooter)
                mSwipeToLoadLayout.isLoadMoreEnabled = false
            }
            page++
        }
        else
        {
            if (page == 1)
            {
                adapter.replaceData(arrayListOf())
                mSwipeToLoadLayout.isLoadMoreEnabled = false
            }
        }
    }

    private var page = 1

    override fun disLoading()
    {
        mSwipeToLoadLayout.isLoadingMore = false
        mSwipeToLoadLayout.isRefreshing = false
    }

    override fun onRefresh()
    {
        adapter.removeAllFooterView()
        page = 1
        mSwipeToLoadLayout.isLoadMoreEnabled = true
        mSwipeToLoadLayout.isRefreshEnabled = true
        getData(true)
    }

    override fun onLoadMore()
    {
        getData(true)
    }
}